package com.leesche.zcsp.node.utils;




import java.security.MessageDigest;

public class MD5Util {

	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };


	public static void main(String[] args){
		String str = "appid=wxd66746c90dac4e8e&attach=10000&body=1元4币&device_info=WEB&mch_id=625478579064286331&nonce_str=f94vuell8x4yjhlew6fowurwqep3usu2&notify_url=http://m.leesche.cn/node/payinfo/notify&openid=o616as4HQlZqDITAKStUcOPzLgd0&out_trade_no=1dd58f32a4274de99195477bfccabddb&service=pay.weixin.jspay&spbill_create_ip=119.130.170.59&total_fee=100&trade_type=JSAPI&key=5C101C971EE837E13DDE2D32A5054AAE";
//		String str2 = "attach=10000&body=1元null币&mch_create_ip=192.168.31.20&mch_id=686478077269536814&nonce_str=xr7oyjwps4ceiuqyjjd1e0yief0017fs&notify_url=http://m.leesche.cn/node/payinfo/notify&out_trade_no=f872f5bc8d2241e4b00936b39db6e936&service=pay.weixin.jspay&sub_openid=597478137321967479&total_fee=100";
//		String key = "5C101C971EE837E13DDE2D32A5054AAE";
		String ret = MD5Encode(str, "utf-8");
		System.out.println(ret.toUpperCase());

//		String ret2 = MD5.sign(str2,key,"utf-8");
//		System.out.println(ret2.toUpperCase());
	}


}
